package autocode

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
	autoCodeReq "github.com/flipped-aurora/gin-vue-admin/server/model/autocode/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
)

type MilkCouponService struct {
}

// CreateMilkCoupon 创建MilkCoupon记录
// Author [piexlmax](https://github.com/piexlmax)
func (milkCouponService *MilkCouponService) CreateMilkCoupon(milkCoupon autocode.MilkCoupon) (err error) {
	err = global.GVA_DB.Create(&milkCoupon).Error
	return err
}

// DeleteMilkCoupon 删除MilkCoupon记录
// Author [piexlmax](https://github.com/piexlmax)
func (milkCouponService *MilkCouponService) DeleteMilkCoupon(milkCoupon autocode.MilkCoupon) (err error) {
	err = global.GVA_DB.Delete(&milkCoupon).Error
	return err
}

// DeleteMilkCouponByIds 批量删除MilkCoupon记录
// Author [piexlmax](https://github.com/piexlmax)
func (milkCouponService *MilkCouponService) DeleteMilkCouponByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]autocode.MilkCoupon{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateMilkCoupon 更新MilkCoupon记录
// Author [piexlmax](https://github.com/piexlmax)
func (milkCouponService *MilkCouponService) UpdateMilkCoupon(milkCoupon autocode.MilkCoupon) (err error) {
	err = global.GVA_DB.Save(&milkCoupon).Error
	return err
}

// GetMilkCoupon 根据id获取MilkCoupon记录
// Author [piexlmax](https://github.com/piexlmax)
func (milkCouponService *MilkCouponService) GetMilkCoupon(id uint) (err error, milkCoupon autocode.MilkCoupon) {
	err = global.GVA_DB.Where("id = ?", id).First(&milkCoupon).Error
	return
}

// GetMilkCouponInfoList 分页获取MilkCoupon记录
// Author [piexlmax](https://github.com/piexlmax)
func (milkCouponService *MilkCouponService) GetMilkCouponInfoList(info autoCodeReq.MilkCouponSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&autocode.MilkCoupon{}).Order("is_delete")
	var milkCoupons []autocode.MilkCoupon
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.IsDelete != nil {
		db = db.Where("is_delete = ?", info.IsDelete)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Limit(limit).Offset(offset).Find(&milkCoupons).Error
	return err, milkCoupons, total
}
